package service;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.PageSql;
import model.DaliyRecord;
import model.DaliyRecords;
import model.Staff;
import model.Station;

public class DaliyRecordService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tdr.id,tdr.createTime,tdr.isNormal,tdr.remark,tdi.title,tdi.content,tdi.duration,ts.name,tc.name AS companyName1,tsr.name AS servicerName,tdr.recorder " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_daliy_record tdr ") ;
	    pageSql.join(" t_daliy_items tdi " , " tdi.id = tdr.daliyItemsId ") ;
	    pageSql.join(" t_station ts " , " ts.id = tdr.stationId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tdr.servicerId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
		pageSql.setNotLessThanCondition("tdr.createTime","tdr.createTime.start") ; //创建时间
		pageSql.setNotMoreThanCondition("tdr.createTime","tdr.createTime.end") ; //创建时间
		pageSql.setEqualCondition("tdr.isNormal") ; //是否正常	 
		pageSql.setEqualCondition("tdr.stationId") ; //点位	 
		pageSql.setLikeCondition("ts.name") ; //点位名称	 
		pageSql.setLikeCondition("tsr.name") ; //运维公司	 
		pageSql.setLikeCondition("tc.name") ; //企业名称	 
	    pageSql.orderBy(" tdr.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tdr.id,tdr.createTime,tdr.isNormal,tdr.remark,tdr.recorder,ts.name " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_daliy_records tdr ") ;
	    pageSql.join("t_daliy_record tdd", "tdd.recorderId = tdr.id");
	    pageSql.join(" t_servicer ts " , " ts.id = tdr.servicerId ") ;
		pageSql.setEqualCondition("tdr.stationId") ;
		pageSql.setEqualCondition("tdd.daliyItemsId");
		pageSql.setNotLessThanCondition("tdr.createTime", "startTime");
		pageSql.setNotMoreThanCondition("tdr.createTime", "endTime");
		pageSql.groupBy(" tdr.id ") ;
	    pageSql.orderBy(" tdr.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		List<Record> list = page.getList();
		for (Record record : list) {
			List<Record> items = DaliyRecord.dao.findByRecordsId( record.getLong("id") );
			record.set("list", items);
		}
		return page ;
	}

	public Ret switchIsNormal(Long id) {
		DaliyRecord daliyRecord = DaliyRecord.dao.findById(id);
		if (null == daliyRecord) {
			return fail();
		}
		daliyRecord.setIsNormal(!daliyRecord.getIsNormal());
		return daliyRecord.update() ? ok() : fail() ;
	}

	public Ret save(DaliyRecord daliyRecord , Station station , Staff staff) {
		daliyRecord.setStationId(station.getId());
		daliyRecord.setServicerId(staff.getServicerId());
		daliyRecord.setRecorder(staff.getName());
//		daliyRecord.setType(station.getType());
		daliyRecord.save();
	    return ok() ;
	}
	
	public Ret batchSave(List<DaliyRecord> list , DaliyRecords daliyRecords , String factors , Integer days , Station station , Staff staff) {
		//保存总的运维记录
		daliyRecords.setStationId(station.getId());
		daliyRecords.setServicerId(staff.getServicerId());
		daliyRecords.setRecorder(staff.getName());
//		daliyRecords.setType(station.getType());
		daliyRecords.save();
		//保存维护项目的运维记录
		for (DaliyRecord daliyRecord : list) {
			daliyRecord.setRecorderId(daliyRecords.getId());
			daliyRecord.setStationId(station.getId());
			daliyRecord.setServicerId(staff.getServicerId());
			daliyRecord.setRecorder(staff.getName());
//			daliyRecord.setType(station.getType());
		}
		Db.batchSave(list, list.size());
		//保存最近一次维护记录
		new MaintainRecordsService().saveMaintain(daliyRecords, factors, days, station, staff);
		return ok() ;
	}
}
